//
//  YQLoginTableViewCell.m
//  YQLoginDemo
//
//  Created by weixb on 2018/2/6.
//  Copyright © 2018年 YQHD. All rights reserved.
//

#import "YQLoginTableViewCell.h"
#import <Masonry.h>

@implementation YQLoginTableViewCell

#pragma mark - initialize
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self initialize];
        [self setup];
        [self autoLayout];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

#pragma mark - private
- (void)initialize {
    
}
- (void)setup {
    [self.contentView addSubview:self.iconView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.textField];
    [self.contentView addSubview:self.button];
    [self.contentView addSubview:self.line];
}
- (void)autoLayout {
    UIView *contentView = self.contentView;
    
    CGSize iconSize = self.iconView.image.size;
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(0);
        make.width.mas_equalTo(iconSize.width);
        make.centerY.equalTo(contentView);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.iconView.mas_right);
        make.centerY.equalTo(contentView);
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right).offset(12);
        make.centerY.equalTo(contentView);
        make.right.equalTo(self.button.mas_left);
    }];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(0);
        make.centerY.equalTo(contentView);
        if (!_button.titleLabel.text.length) {
            make.width.mas_equalTo(0);
        }
    }];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_offset(0);
        make.height.mas_equalTo(1);
    }];
    [self.titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.button setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
}

#pragma mark - getter
- (UIImageView *)iconView {
    if (!_iconView) {
        _iconView = [UIImageView new];
        _iconView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _iconView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = self.font;
        _titleLabel.textColor = self.textColor;
    }
    return _titleLabel;
}
- (UITextField *)textField {
    if (!_textField) {
        _textField = [UITextField new];
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.font = self.font;
        _textField.textColor = self.textColor;
    }
    return _textField;
}
- (UIButton *)button {
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button setTitleColor:self.textColor forState:UIControlStateNormal];
        _button.titleLabel.font = self.font;
    }
    return _button;
}
- (UIView *)line {
    if (!_line) {
        _line = [UIView new];
        _line.backgroundColor = [UIColor lightGrayColor];
    }
    return _line;
}

- (UIFont *)font {
    return [UIFont systemFontOfSize:16];
}

- (UIColor *)textColor {
    return [UIColor blackColor];
}

@end
